# AISH (AI Shell) Makefile
# 跨平台编译和开发工具

# 项目配置
APP_NAME := aish
SOURCE_FILE := cmd/main.go
OUTPUT_DIR := dist
BINARY_UNIX := $(OUTPUT_DIR)/$(APP_NAME)
BINARY_WIN := $(OUTPUT_DIR)/$(APP_NAME).exe

# 版本信息
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
GO_VERSION := $(shell go version | awk '{print $$3}')

# 编译选项
LDFLAGS := -s -w
BUILD_FLAGS := -trimpath -ldflags="$(LDFLAGS)"
DEV_FLAGS := -race -trimpath

# 检测操作系统
ifeq ($(OS),Windows_NT)
    DETECTED_OS := Windows
    BUILD_SCRIPT := build.bat
else
    DETECTED_OS := $(shell uname -s)
    BUILD_SCRIPT := ./build.sh
endif

# 默认目标
.PHONY: all
all: info build-all

# 显示项目信息
.PHONY: info
info:
	@echo "=== AISH 编译信息 ==="
	@echo "项目名称: $(APP_NAME)"
	@echo "版本:     $(VERSION)"
	@echo "构建时间: $(BUILD_TIME)"
	@echo "Go版本:   $(GO_VERSION)"
	@echo "检测系统: $(DETECTED_OS)"
	@echo "========================"

# 编译所有平台
.PHONY: build-all
build-all:
	@echo "编译所有平台..."
	@$(BUILD_SCRIPT) all

# 编译Windows平台
.PHONY: build-windows windows
build-windows windows:
	@echo "编译Windows平台..."
	@$(BUILD_SCRIPT) windows

# 编译Linux平台  
.PHONY: build-linux linux
build-linux linux:
	@echo "编译Linux平台..."
	@$(BUILD_SCRIPT) linux

# 编译macOS平台
.PHONY: build-macos macos darwin
build-macos macos darwin:
	@echo "编译macOS平台..."
	@$(BUILD_SCRIPT) macos

# 编译当前平台
.PHONY: build-current build
build-current build:
	@echo "编译当前平台..."
	@$(BUILD_SCRIPT) current

# 开发模式编译
.PHONY: dev
dev:
	@echo "开发模式编译当前平台..."
	@$(BUILD_SCRIPT) --dev current

# 快速编译（当前平台，不优化）
.PHONY: quick
quick:
	@echo "快速编译..."
	@go build $(DEV_FLAGS) -o $(APP_NAME) $(SOURCE_FILE)

# 运行程序
.PHONY: run
run:
	@echo "运行AISH..."
	@go run $(SOURCE_FILE)

# 安装到 GOPATH/bin
.PHONY: install
install:
	@echo "安装到系统..."
	@go build $(BUILD_FLAGS) -o $(APP_NAME) $(SOURCE_FILE)
	@mkdir -p $$(go env GOBIN)
	@cp $(APP_NAME) $$(go env GOBIN)/$(APP_NAME)
	@rm $(APP_NAME)
	@echo "已安装到: $$(go env GOBIN)/$(APP_NAME)"

# 清理构建文件
.PHONY: clean
clean:
	@echo "清理构建文件..."
	@$(BUILD_SCRIPT) --clean
	@rm -f $(APP_NAME) $(APP_NAME).exe

# 深度清理（包括依赖缓存）
.PHONY: clean-all
clean-all: clean
	@echo "清理Go模块缓存..."
	@go clean -modcache
	@go clean -cache

# 代码格式化
.PHONY: fmt
fmt:
	@echo "格式化代码..."
	@go fmt ./...

# 代码检查
.PHONY: vet
vet:
	@echo "静态分析..."
	@go vet ./...

# 运行测试
.PHONY: test
test:
	@echo "运行测试..."
	@go test -v ./...

# 测试覆盖率
.PHONY: test-coverage
test-coverage:
	@echo "测试覆盖率..."
	@go test -coverprofile=coverage.out ./...
	@go tool cover -html=coverage.out -o coverage.html
	@echo "覆盖率报告: coverage.html"

# 代码质量检查
.PHONY: lint
lint: fmt vet
	@echo "代码质量检查完成"

# 更新依赖
.PHONY: deps
deps:
	@echo "更新依赖..."
	@go mod download
	@go mod tidy
	@go mod verify

# 生成版本信息
.PHONY: version
version:
	@echo "应用版本: $(VERSION)"
	@echo "构建时间: $(BUILD_TIME)"
	@echo "Go版本:   $(GO_VERSION)"

# 创建发布包
.PHONY: release
release: clean build-all
	@echo "创建发布包..."
	@cd $(OUTPUT_DIR) && find . -name "$(APP_NAME)*" -type f | while read file; do \
		dir=$$(dirname "$$file"); \
		base=$$(basename "$$file"); \
		if [[ "$$file" == *".exe" ]]; then \
			zip -j "$(APP_NAME)-$(VERSION)-$${dir//\//-}.zip" "$$file"; \
		else \
			tar -czf "$(APP_NAME)-$(VERSION)-$${dir//\//-}.tar.gz" -C "$$dir" "$$base"; \
		fi; \
	done
	@echo "发布包创建完成: $(OUTPUT_DIR)/"

# 检查构建环境
.PHONY: check-env
check-env:
	@echo "检查构建环境..."
	@echo "Go版本: $$(go version)"
	@echo "GOPATH: $$(go env GOPATH)"
	@echo "GOROOT: $$(go env GOROOT)"
	@echo "GOOS: $$(go env GOOS)"
	@echo "GOARCH: $$(go env GOARCH)"
	@echo "CGO_ENABLED: $$(go env CGO_ENABLED)"

# 显示帮助信息
.PHONY: help
help:
	@echo "AISH 编译系统帮助"
	@echo ""
	@echo "构建命令:"
	@echo "  make all          编译所有平台"
	@echo "  make build        编译当前平台"
	@echo "  make windows      编译Windows平台"
	@echo "  make linux        编译Linux平台" 
	@echo "  make macos        编译macOS平台"
	@echo "  make dev          开发模式编译"
	@echo "  make quick        快速编译（不优化）"
	@echo ""
	@echo "开发命令:"
	@echo "  make run          直接运行程序"
	@echo "  make install      安装到系统"
	@echo "  make test         运行测试"
	@echo "  make lint         代码检查"
	@echo "  make fmt          格式化代码"
	@echo ""
	@echo "维护命令:"
	@echo "  make clean        清理构建文件"
	@echo "  make deps         更新依赖"
	@echo "  make release      创建发布包"
	@echo "  make version      显示版本信息"
	@echo "  make check-env    检查构建环境"
	@echo ""
	@echo "更多信息请查看: README.md"
